package com.longxi.tianjintianbao.ui.presenter.approval;

import android.content.Intent;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.approval.Approval;
import com.longxi.tianjintianbao.model.approval.ApprovalContent;
import com.longxi.tianjintianbao.model.approval.ApprovalContentList;
import com.longxi.tianjintianbao.ui.activity.approval.ApprovalSaveActivity;
import com.longxi.tianjintianbao.ui.activity.assetscheck.AssetsContentActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.approval.ApprovalContentListViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.approval.ApprovalContentViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.approval.ApprovalNoTopViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.approval.IApprovalDetailAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ApprovalDetailAtPresenter extends BasePresenter<IApprovalDetailAtView> {

    private MultiTypeAdapter adapter;
    private Items items = new Items();
    private Approval approval;

    public ApprovalDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Approval.class, new ApprovalNoTopViewBinder(listener));
            adapter.register(ApprovalContent.class, new ApprovalContentViewBinder(listener));
            adapter.register(ApprovalContentList.class, new ApprovalContentListViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
            adapter.setItems(items);
        }
    }

    public void setBar() {
        mContext.setTitle("审批详情");
        mContext.setTvLeft(MyUtils.getString(R.string.back));

        approval = (Approval) mContext.getIntent().getSerializableExtra(AppConst.APPROVAL);
    }

    public void setDate() {
        getView().getLlAudit().setVisibility(View.VISIBLE);
    }

    /**
     * 测试数据
     */
    public void getTaskList(){
        JSONObject object = new JSONObject();
        try {
            object.put("billid", approval.getBillid());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().myApprovalDetail(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<ApprovalContent>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<ApprovalContent> result) {
                if(result.getCode() != 0){
                    mContext.showError();
                    return;
                }
                items.clear();
                items.add(approval);
                if(approval.getState().equals("正在办理")){
                    setDate();
                }
                for (ApprovalContent approvalContent : result.getData()){
                    items.add(approvalContent);
                    items.addAll(approvalContent.getHouses());
                }

                adapter.notifyDataSetChanged();
                mContext.hideLoading();
            }
        });
    }

    AdapterListener listener = new AdapterListener() {

        @Override
        public void onClick(int position) {
            Intent intent = new Intent(mContext, AssetsContentActivity.class);
            mContext.startActivity(intent);
        }

        @Override
        public void onDelete(int position) {

        }
    };

    /**
     *
     * @param check 审批 0-通过 1-驳回
     */
    public void myApprovalSave(String check){
        Intent intent = new Intent(mContext, ApprovalSaveActivity.class);
        intent.putExtra(AppConst.BILLID, approval.getBillid());
        intent.putExtra(AppConst.CHECK, check);
        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_APPROVAL_SAVE);

//        JSONObject object = new JSONObject();
//        try {
//            object.put("billid", approval.getBillid());
//            object.put("userid", MyApplication.loginUser.userid);
//            object.put("check", check);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//        ApiRetrofit.getInstance().myApprovalSave(object)
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new MySubscriber<BaseResponse>(mContext) {
//            @Override
//            public void onError(ExceptionHandle.ResponeThrowable throwable) {
//                mContext.hideWaitingDialog();
//                MyUtils.showToast(throwable.message);
//            }
//
//            @Override
//            public void onNext(BaseResponse result) {
//                if(result.getCode() != 0){
//                    MyUtils.showToast(result.getMsg());
//                    return;
//                }
//                mContext.setResult(mContext.RESULT_OK);
//                mContext.finish();
//            }
//        });
    }

    @Override
    public void detachView() {
        super.detachView();
        this.adapter = null;
        this.items = null;
        this.listener = null;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != mContext.RESULT_OK){
            return;
        }
        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_APPROVAL_SAVE:
                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
                break;
        }
    }
}
